package com.site.wisdom_construction.controller.personnelModule;

import com.site.wisdom_construction.entity.personnelModule.SpecialPersonnel;
import com.site.wisdom_construction.entity.personnelModule.TemporaryWorkers;
import com.site.wisdom_construction.responseEntity.JsonResult;
import com.site.wisdom_construction.responseEntity.OneDataResult;
import com.site.wisdom_construction.responseEntity.Page;
import com.site.wisdom_construction.service.personnelModule.TemporaryWorkersService;
import com.site.wisdom_construction.util.TimeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author daiyifei
 * @Date 2022/1/5
 * @Description
 */
@Api(tags = "临时工信息接口")
@RestController
@RequestMapping("TemporaryWorkers")
@CrossOrigin
public class TemporaryWorkersController {
    @Autowired
    private TemporaryWorkersService temporaryWorkersService;

    @ApiOperation(value = "增加临时工信息", notes = "测试数据:")
    @PostMapping("/insertTemporaryWorkers")
    public JsonResult insertTemporaryWorkers(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        List<TemporaryWorkers> temporaryWorkersList = temporaryWorkersService.findTemporaryWorkersByCard(map);
        if (temporaryWorkersList.size() > 0) {
            jsonResult.setCode(200);
            jsonResult.setMessage("临时工已存在!");
        } else {
            int i = temporaryWorkersService.insertTemporaryWorkers(map);
            if (i == 1) {
                jsonResult.setCode(200);
                jsonResult.setMessage("成功录入临时工!");
            } else {
                jsonResult.setCode(20006);
                jsonResult.setMessage("录入临时工失败!");
            }
        }
        return jsonResult;
    }

    @ApiOperation(value = "删除临时工信息", notes = "测试数据:")
    @PostMapping("/deleteTemporaryWorkers")
    public JsonResult deleteTemporaryWorkers(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        List<Integer> list = (List<Integer>) map.get("list");
        int l = 0;
        Map mapR = new HashMap();
        for (int i = 0;i<list.size();i++){
            mapR.put("id",list.get(i));
            int a = temporaryWorkersService.deleteTemporaryWorkers(mapR);
            l+=a;
        }
        if (l>=1) {
            jsonResult.setCode(200);
            jsonResult.setMessage("成功删除临时工!");
        } else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("删除临时工失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "修改临时工信息", notes = "测试数据:")
    @PostMapping("/updateTemporaryWorkers")
    public JsonResult updateTemporaryWorkers(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        int i = temporaryWorkersService.updateTemporaryWorkers(map);
        if (i == 1) {
            jsonResult.setCode(200);
            jsonResult.setMessage("成功修改临时工!");
        } else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("修改临时工失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "临时工退场", notes = "测试数据:")
    @PostMapping("/contractTemporaryWorkersExeunt")
    public JsonResult contractTemporaryWorkersExeunt(@RequestBody Map map) {
        map.put("leaveDate", TimeUtils.getNowTime());
        JsonResult jsonResult = new JsonResult();
        int i = temporaryWorkersService.contractTemporaryWorkersExeunt(map);
        if (i == 1) {
            jsonResult.setCode(200);
            jsonResult.setMessage("临时工退场成功!");
        } else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("临时工退场失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "临时工进场", notes = "测试数据:")
    @PostMapping("/contractTemporaryWorkersCourse")
    public JsonResult contractTemporaryWorkersCourse(@RequestBody Map map) {
        map.put("entryDate", TimeUtils.getNowTime());
        JsonResult jsonResult = new JsonResult();
        int i = temporaryWorkersService.contractTemporaryWorkersCourse(map);
        if (i == 1) {
            jsonResult.setCode(200);
            jsonResult.setMessage("临时工进场成功!");
        } else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("临时工进场失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页模糊查询临时工信息", notes = "测试数据:")
    @PostMapping("/findTemporaryWorkersByPage")
    public Page findTemporaryWorkersByPage(@RequestBody Map map) {
        Page page = new Page();
        page.setPageNo((Integer) map.get("pageNo"));
        page.setPageSize((Integer) map.get("pageSize"));
        page.setTotal(temporaryWorkersService.Total(map));
        page.setItems(temporaryWorkersService.findTemporaryWorkersByPage(map));
        return page;
    }

    @ApiOperation(value = "根据ID查询临时工信息", notes = "测试数据:")
    @PostMapping("/findTemporaryWorkersById")
    public OneDataResult selectProjectProgressPlan(@RequestBody Map map) {
        OneDataResult oneDataResult = new OneDataResult();
        oneDataResult.setData(temporaryWorkersService.findTemporaryWorkersById(map));
        return oneDataResult;
    }
}
